'''

    The ConrolMap must be created by a Device and set to a KeyGen
    when they are associated.

    See ControlMap() class

'''

from SHIK.keygens._abstract import Event

class ControlMap(object):
    def __init__(self, push_control_to_event):
        '''
        The ControlMap is mainly used to convert PuchControl to Event.
        When a device uses a KeyGen, it must specify a ControlMap that
        uses only PushControl available on it and Events handled by the
        KeyGen.
        
        The push_control_to_event must be a dict like:
            {SHIK.devices._abstract.PushControl: SHIK.keygens._abstract.Event}
        
        '''
        object.__init__(self)
        self._pc_to_evt = push_control_to_event.copy()
        
    def push_control_to_event(self):
        '''
        Return a dict like {Event: PushControl}
        containing all the PushControl handled by this
        ControlMap.
        '''
        return self._pc_to_evt
    
    def used_push_controls(self):
        '''
        Returns the list of PushControl used by
        this ControlMap.
        '''
        return self.push_control_to_event().keys()
    
    def convert_push_control(self, pc):
        '''
        The KeyGen calls this converter to store event corresponding
        to the PushControl it received.
        
        Default implementation is to return Event.Unknown when the
        PushControl is not used by the ControlMap.
        
        Subclasses could choose to return None, meaning that the 
        PushControl should be ignored.
        '''
#        print 'Convert Push Control:', repr(pc)
#        print '    ', self._pc_to_evt
        ret = self.push_control_to_event().get(pc,Event.Unknown)
#        print '  ->', ret
        return ret
